function FundIndexController($state, $scope, $http) {

    function getFundList() {
        $http({
            url: 'fund/getFundList',
            method: 'get'
        }).success(function(response){
            if (response == "") {
                $scope.hasFund = false;
            }
            else {
                $scope.hasFund = true;
                for(var i = 0; i < response.length; i++) {
                    response[i].createDate = (new Date(response[i].createDate)).toLocaleString();
                    response[i].operatingPeriod = (new Date(response[i].operatingPeriod)).toLocaleString();
                }
                $scope.fundList = response;
            }
        })
    }

    getFundList();

}

function FundAddController($state, $scope, $http) {
    function getList(){
        $http({
            url: '/stock/getDistinctList',
            method: 'get',
        }).success(function (response) {
            $scope.list = response;
        })
    }

    getList();

    $scope.add = function () {
        var quantityArray = new Array;
        for(var i = 0; i < $scope.list.length; i++) {
            console.log();
            var val = $("#quantity" + i).val();
            if (val == null || val == "" || val == "undefined") {
                quantityArray[i] = 0;
            }
            else {
                quantityArray[i] = val;
            }
        }

        $http({
            url: 'fund/add',
            method: 'post',
            data: {
                name: $scope.name,
                initialShares: $scope.initialShares,
                cash: $scope.cash
            },
            params: {
                quantity: quantityArray
            }
        }).success(function(response){
            $state.go("index.fund")
        })
    };
}

function FundEditController($stateParams, $scope, $http, $state) {
    var fundUid = $stateParams.fundUid;

    console.log(fundUid);

    function getFundByFundUid(fundUid) {
        $http({
            url: 'fund/getFund',
            method: 'get',
            params: {
                fundUid: fundUid
            }
        }).success(function (response) {
            $scope.fund = response;
            console.log($scope.fund.fundStocks);
            console.log($scope.fund.fundStocks[0].name);
            console.log(response)
        })
    }

    $scope.modify = function() {
        console.log($scope.fund);

        var sellQuantityArray = new Array;
        var buyQuantityArray = new Array;
        for(var i = 0; i < $scope.fund.fundStocks.length; i++) {
            var sellVal = $("#sell" + i).val();
            if (sellVal == null || sellVal == "" || sellVal == "undefined") {
                sellQuantityArray[i] = 0;
            }
            else {
                sellQuantityArray[i] = sellVal;
            }

            var buyVal = $("#buy" + i).val();
            if (buyVal == null || buyVal == "" || buyVal == "undefined") {
                buyQuantityArray[i] = 0;
            }
            else {
                buyQuantityArray[i] = buyVal;
            }
        }

        console.log(sellQuantityArray);
        console.log(buyQuantityArray);

        $http({
            url: 'fund/modify',
            method: 'post',
            params: {
                fundUid: fundUid,
                sellQuantity: sellQuantityArray,
                buyQuantity: buyQuantityArray
            }
        }).success(function () {
            $state.go("index.fund");
        })
    }

    getFundByFundUid(fundUid);

}